home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Background ppat 1.0.1 / src / main.c < prev    next >
Encoding:
Text File  |  1995-12-15  |  5.1 KB  |  208 lines  |  [TEXT/CWIE]

  1.  
  2. /* ----------------------------------------------------------------------
  3. InitToolBox
  4. ---------------------------------------------------------------------- */
  5. void InitToolBox()
  6. {
  7.     InitGraf(&qd.thePort);
  8.     InitFonts();
  9.     FlushEvents(everyEvent, 0);
  10.     InitWindows();
  11.     InitMenus();
  12.     TEInit();
  13.     InitDialogs(0L);
  14.     InitCursor();
  15. }
  16.  
  17.  
  18. /* ----------------------------------------------------------------------
  19. InitMenuBar
  20. ---------------------------------------------------------------------- */
  21. void InitMenuBar()
  22. {
  23.     Handle    myMenuBar;
  24.     
  25.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  26.     if (myMenuBar == NIL)
  27.         SysBeep(1);
  28.     else
  29.         SetMenuBar(myMenuBar);
  30.     
  31.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  32.     if (gAppleMenu == NIL)
  33.         SysBeep(1);
  34.     else
  35.         AddResMenu(gAppleMenu,'DRVR');
  36.     
  37.     gFileMenu = GetMHandle(MENU_FILE_ID);
  38.     if (gFileMenu == NIL)
  39.         SysBeep(1);
  40.  
  41.     DrawMenuBar();
  42. }
  43.  
  44.  
  45. /* ----------------------------------------------------------------------
  46. MainLoop
  47. ---------------------------------------------------------------------- */
  48. void MainLoop()
  49. {
  50.     RgnHandle        cursorRgn;
  51.     Boolean            gotEvent;
  52.     WindowRecord    *myWinRecord;
  53.     Rect            myWinRect;
  54.     Str255             myTitle;
  55.     
  56.     gInBackground = false;
  57.     
  58.     cursorRgn = NewRgn();
  59.     
  60.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  61.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  62.  
  63.     gPixPat = GetPixPat(SYSTEM_PPAT);
  64.  
  65.     myWinRect = qd.screenBits.bounds;
  66.     myWinRecord = NIL;
  67.     gTheWindow =
  68.         NewCWindow(myWinRecord,&myWinRect,"\p",FALSE,plainDBox,(WindowPtr)-1L,FALSE,0L);
  69.     
  70.     if (gTheWindow)
  71.     {
  72.         ShowWindow(gTheWindow);
  73.  
  74.         gDone = false;
  75.  
  76.         while (gDone == false)
  77.         {
  78.             if (gWNEImplemented)
  79.                 gotEvent = WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  80.             else
  81.             {
  82.                 SystemTask();
  83.                 gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  84.             }
  85.         
  86.             if (gotEvent)
  87.                 Do();
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. /* ----------------------------------------------------------------------
  94. Do
  95. ---------------------------------------------------------------------- */
  96. void Do()
  97. {
  98.     char    c;
  99.     
  100.     switch (gTheEvent.what)
  101.     {
  102.         case nullEvent:
  103.             break;
  104.         case mouseDown:
  105.             DoMouseDown();
  106.             break;
  107.         case keyDown:
  108.         case autoKey:
  109.             c = gTheEvent.message & charCodeMask;
  110.             if ((gTheEvent.modifiers & cmdKey) != 0)
  111.             {
  112.                 DoMenu(MenuKey(c));
  113.             }
  114.             break;
  115.         case activateEvt:
  116.             DoUpdate();
  117.             break;
  118.         case updateEvt:
  119.             DoUpdate();
  120.             break;
  121.         case app4Evt:
  122.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  123.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  124.             break;
  125.     }
  126. }
  127.  
  128. /* ----------------------------------------------------------------------
  129. DoUpdate
  130. ---------------------------------------------------------------------- */
  131. void DoUpdate()
  132. {
  133.     GrafPtr            savedPort;
  134.     Rect            theRect;
  135.     
  136.     SetCursor(&qd.arrow);
  137.     GetPort(&savedPort);
  138.     SetPort(gTheWindow);
  139.     BeginUpdate(gTheWindow);
  140.         SetRect(&theRect,gTheWindow->portRect.left,
  141.             gTheWindow->portRect.top,
  142.             gTheWindow->portRect.right,
  143.             gTheWindow->portRect.bottom);
  144.         if (gPixPat != NIL)
  145.             FillCRect(&theRect,gPixPat);
  146.     EndUpdate(gTheWindow);
  147.     SetPort(savedPort);
  148. }
  149.  
  150. /* ----------------------------------------------------------------------
  151. DoMouseDown
  152. ---------------------------------------------------------------------- */
  153. void DoMouseDown()
  154. {
  155.     WindowPtr    window;
  156.     short int    thePart;
  157.     long int    menuChoice, windSize, newSize;
  158.     Boolean        doZoom, doGoAway;
  159.     Point        p;
  160.     
  161.     thePart = FindWindow(gTheEvent.where,&window);
  162.     switch (thePart)
  163.     {
  164.         case inMenuBar:
  165.             menuChoice = MenuSelect(gTheEvent.where);
  166.             DoMenu(menuChoice);
  167.             break;
  168.         case inSysWindow:
  169.             SystemClick(&gTheEvent,window);
  170.             break;
  171.         case inContent:
  172.             if (window != FrontWindow())
  173.                 SelectWindow(window);
  174.             break;
  175.         default:
  176.             break;
  177.     }
  178. }
  179.  
  180. /* ----------------------------------------------------------------------
  181. DoMenu
  182. ---------------------------------------------------------------------- */
  183. void DoMenu(menuChoice)
  184. long int menuChoice;
  185. {
  186.     int    theMenu;
  187.     int    theItem;
  188.     
  189.     if (menuChoice != 0)
  190.     {
  191.         theMenu = HiWord(menuChoice);
  192.         theItem = LoWord(menuChoice);
  193.         switch (theMenu)
  194.         {
  195.             case MENU_APPLE_ID:
  196.                 DoMenuApple(theItem);
  197.                 break;
  198.             case MENU_FILE_ID:
  199.                 DoMenuFile(theItem);
  200.                 break;
  201.             default:
  202.                 break;
  203.         }
  204.         HiliteMenu(0);
  205.     }
  206. }
  207.